
Public Class AccessHtml
    Private mDelay As Long = 750
    Public Property Delay() As Long
        Get
            Return mDelay
        End Get
        Set(ByVal value As Long)
            If value >= 0 Then
                mDelay = value
            End If
        End Set
    End Property

    Public Function GetHtmlData(ByVal url As String) As String

        Dim WC As System.Net.WebClient = New System.Net.WebClient()
        WC.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows XP)")

        'My.Application.DoEvents()
        Dim data As Byte() = Nothing
        Try
            data = WC.DownloadData(url)
        Catch ex As Exception
            Return ""
        End Try

        Dim enc As System.Text.Encoding = System.Text.Encoding.UTF8

        'My.Application.DoEvents()
        Dim text As String = ""
        Try
            text = enc.GetString(data)
        Catch ex As Exception
            Return ""
        End Try

        'My.Application.DoEvents()
        System.Threading.Thread.Sleep(mDelay)

        Return text
    End Function

    Public Function GetStringFromDataStructure(ByVal html As String, ByVal dataName As String, Optional ByVal suffix As String = "]]") As String
        If InStr(html, dataName & "[") = 0 Or InStr(html, suffix) = 0 Then
            Return ""
        End If

        html = Replace(html, vbCr, "")
        html = Replace(html, vbLf, "")

        html = Microsoft.VisualBasic.Mid(html, InStr(html, dataName & "[") + Len(dataName & "["))
        html = Microsoft.VisualBasic.Left(html, InStr(html, suffix))

        html = Replace(html, "],[", vbCrLf)
        html = Replace(html, "], [", vbCrLf)
        html = Replace(html, "]", "")
        html = Replace(html, "[", "")

        Return html
    End Function

    Public Function RemoveParenthesis(ByVal str As String) As String
        Do While InStr(str, "(") > 0 And InStr(str, ")") > 0 And InStr(str, "(") < InStr(str, ")")
            str = Left(str, InStr(str, "(") - 1) & Mid(str, InStr(str, ")") + 1)
        Loop

        Return str
    End Function

    Public Function GetNumberString(ByVal str As String) As String
        Dim s As String

        s = ReplaceSeparater(Replace(RemoveDblQuote(RemoveTag(str)), "%", ""))

        If s = "" Then
            Return "0"
        Else
            Return s
        End If
    End Function

    Public Function ReplaceSeparater(ByVal str As String) As String
        Return Replace(Replace(str, " ", ""), ",", "")
    End Function

    Public Function RemoveTag(ByVal str As String) As String
        Do While InStr(str, "<") > 0 And InStr(str, ">") > 0 And InStr(str, "<") < InStr(str, ">")
            str = Left(str, InStr(str, "<") - 1) & Mid(str, InStr(str, ">") + 1)
        Loop

        Return str
    End Function

    Public Function RemoveDblQuote(ByVal str As String) As String
        Return RemoveCharacter(RemoveCharacter(str, " "), """")
    End Function

    Public Function RemoveQuote(ByVal str As String) As String
        Return RemoveCharacter(RemoveCharacter(str, " "), "'")
    End Function

    Public Function RemoveCharacter(ByVal str As String, ByVal chara As String) As String
        If str = "" Then Return ""

        Do While InStr(str, chara) = 1
            str = Mid(str, 2)
        Loop

        If str = "" Then Return ""

        Do While Right(str, 1) = chara
            If Len(str) = 1 Then
                str = ""
            Else
                str = Mid(str, 1, Len(str) - 1)
            End If
        Loop

        Return str
    End Function
End Class
